// service层
import User from "../model/User"

class UserService {
    getUserById(id: number) {
        return User.findByPk(id)
    }

    getUserListByPage(page: number = 1, limit: number = 15) {
        /* 关键在于对应sql语句
            SELECT * FROM users
            LIMIT 3
            OFFSET 3
         */
        return User.findAndCountAll({
            limit: limit,
            offset: (page - 1) * limit
        })
    }

    getUserByName(name: string) {
        return User.findOne({
            where: {
                name: name
            }
        })
    }

    addUser(admin: any) {
        return User.create(admin)
    }

    updateUser(id: number, admin: any) {
        return User.update(admin, { where: { id } })
    }

    deleteUser(id: number) {
        return User.destroy({ where: { id } })
    }
}

export default new UserService